import { showAlert } from "@util/showAlert"
import { useFileStore } from "@store/useFileStore"
import { getLocal } from "./index"

export default function handleFile(fileList: FileList | undefined | null) {
  const language = getLocal("__LANGUAGE__")

  if (!fileList) {
    language === "cn" ? showAlert("error", `系统出现问题，请反馈问题`) : showAlert("error", `There is a problem with the system, please report the problem`)
    return
  }

  const fileArr = Array.from(fileList)

  const filrStore = useFileStore()
  const { saveFile } = filrStore

  // 1 判断是不是可取文件
  let bool = fileArr.every(file => {
    return /image/.test(file.type)
  })

  if (!bool) {
    language === "cn" ? showAlert("error", `不是受支持的 image 格式`) : showAlert("error", `UnSupported image type`)
    return
  }

  // 2 生成对象存进数组
  saveFile(fileArr)
};
